/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kpsmart.gui;

import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.event.DocumentListener;
import kpsmart.model.events.Events;
import kpsmart.model.routes.Routes;
import org.joda.time.DateTime;

/**
 * Dialog to send a new mail item
 * @author Oliver Behncke
 */
public class NewMailForm extends javax.swing.JFrame {

    /**
     * Creates new form NewMailForm
     */
    NewMailForm() {
        initComponents();
        this.jTextFieldDay.setText(Routes.dayForNo(DateTime.now().getDayOfWeek()));
    }
    
    /**
     * Add the same FocusListener to weight and volume textfield
     * @param l 
     */
    void addFocusListenerToWandV(FocusListener l){
        this.jFormattedTextFieldVolume.addFocusListener(l);
        this.jFormattedTextFieldWeight.addFocusListener(l);
    }
    
    /**
     * Add ActionListener to cancel button
     * @param l 
     */
    void addListenerToCancel(ActionListener l){
        this.jButtonCancel.addActionListener(l);
    }
    
    /**
     * Add ActionListener to submit button
     * @param l 
     */
    void addListenerToSubmit(ActionListener l){
        this.jButtonSubmit.addActionListener(l);
    }
    
    /**
     * Add ActionListener to from combo box
     * @param l 
     */
    void addListenerToFrom(ActionListener l){
        this.jComboBoxFrom.addActionListener(l);
    }
    
    /**
     * Add ActionListener to "to" combo box
     * @param l 
     */
    void addListenerToTo(ActionListener l){
        this.jComboBoxTo.addActionListener(l);
    }
    
    /**
     * Add ActionListener to priority combo box
     * @param l 
     */
    void addListenerToPriorities(ActionListener l){
        this.jComboBoxPriorities.addActionListener(l);
    }
    
    /**
     * Get String value of selected item of to combo box
     * @return to
     */
    String getTo(){
        return this.jComboBoxTo.getSelectedItem().toString();
    }

    /**
     * Get String value of selected item of from combo box
     * @return from
     */
    String getFrom(){
        return this.jComboBoxFrom.getSelectedItem().toString();
    }
    
    /**
     * Get String value of selected item of priority combo box
     * @return priority
     */
    String getPriority(){
        return this.jComboBoxPriorities.getSelectedItem().toString();
    }
    
    /**
     * Get String value of day text field
     * @return day
     */
    String getDay(){
        return this.jTextFieldDay.getText();
    }

    /**
     * Get int value of weight text field
     * @return weight
     */
    int getWeight(){
        return Integer.valueOf(this.jFormattedTextFieldWeight.getText());
    }
    
    /**
     * Get int value of volume text field
     * @return volume
     */
    int getVolume(){
        return Integer.valueOf(this.jFormattedTextFieldVolume.getText());
    }

    /**
     * Check if weight text field has proper integer value
     * @return boolean
     */
    boolean properWeight(){
        return isProperInteger(this.jFormattedTextFieldWeight.getText());
    }

    /**
     * Check if volume text field has proper integer value
     * @return boolean
     */
    boolean properVolume(){
        return isProperInteger(this.jFormattedTextFieldVolume.getText());
    }
    
    /**
     * Checks if a String has a proper Integer value
     * @param string
     * @return boolean
     */
    private boolean isProperInteger(String string){
        try{
            Integer.valueOf(string);
            return true;
        } catch (NumberFormatException e){
            return false;
        }
    }

    /**
     * Set text of price info label
     * @param info 
     */
    void setPriceInfo(String info){
        this.jLabelPriceInfo.setText(info);
    }
    
    /**
     * Set text of duration info label
     * @param duration 
     */
    void setDuration(String duration){
        this.jLabelDuration.setText(duration);
    }

    /**
     * Set values of priorities combo box
     * @param priorities 
     */
    void setPriorities(Collection<String> priorities){
        DefaultComboBoxModel model=new DefaultComboBoxModel();
        for(String s: priorities) model.addElement(s);
        this.jComboBoxPriorities.setModel(model);
    }
    
    /**
     * Set values of from combo box
     * @param origins 
     */
    void setFrom(Collection<String> origins){
        DefaultComboBoxModel model=new DefaultComboBoxModel();
        for(String s: origins) model.addElement(s);
        this.jComboBoxFrom.setModel(model);
    }
        
    /**
     * Set values of to combo box
     * @param destinations 
     */
    void setTo(Collection<String> destinations){
        DefaultComboBoxModel model=new DefaultComboBoxModel();
        for(String s: destinations) model.addElement(s);
        this.jComboBoxTo.setModel(model);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jFormattedTextFieldVolume = new javax.swing.JFormattedTextField();
        jFormattedTextFieldWeight = new javax.swing.JFormattedTextField();
        jButtonSubmit = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabelPriceInfo = new javax.swing.JLabel();
        jComboBoxPriorities = new javax.swing.JComboBox();
        jComboBoxFrom = new javax.swing.JComboBox();
        jComboBoxTo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabelDuration = new javax.swing.JLabel();
        jTextFieldDay = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("New Mail");
        setAlwaysOnTop(true);
        setMinimumSize(new java.awt.Dimension(295, 338));
        setResizable(false);

        jLabel4.setText("From");

        jLabel5.setText("To");

        jLabel7.setText("Day");

        jLabel9.setText("Volume");

        jLabel10.setText("Weight");

        jFormattedTextFieldVolume.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jFormattedTextFieldVolume.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jFormattedTextFieldVolume.setText("0");

        jFormattedTextFieldWeight.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jFormattedTextFieldWeight.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jFormattedTextFieldWeight.setText("0");

        jButtonSubmit.setText("Submit");

        jButtonCancel.setText("Cancel");

        jLabel13.setText("Priority");

        jLabel1.setText("Price");

        jLabelPriceInfo.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        jComboBoxPriorities.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBoxFrom.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBoxTo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Duration");

        jLabelDuration.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        jTextFieldDay.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonSubmit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 126, Short.MAX_VALUE)
                                .addComponent(jButtonCancel))
                            .addComponent(jLabelDuration, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel9)
                            .addComponent(jLabel13)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel10)
                            .addComponent(jLabel1))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addComponent(jLabelPriceInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
                            .addComponent(jFormattedTextFieldWeight, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jComboBoxPriorities, 0, 279, Short.MAX_VALUE)
                                    .addComponent(jComboBoxFrom, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jComboBoxTo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jFormattedTextFieldVolume)
                                    .addComponent(jTextFieldDay))
                                .addGap(0, 2, Short.MAX_VALUE)))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jComboBoxFrom, jComboBoxPriorities, jComboBoxTo, jFormattedTextFieldVolume, jFormattedTextFieldWeight, jTextFieldDay});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBoxFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBoxTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jComboBoxPriorities, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jFormattedTextFieldVolume, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jFormattedTextFieldWeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelPriceInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2)
                    .addComponent(jLabelDuration, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSubmit)
                    .addComponent(jButtonCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jComboBoxFrom, jComboBoxPriorities, jComboBoxTo, jFormattedTextFieldVolume, jFormattedTextFieldWeight, jTextFieldDay});

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonSubmit;
    private javax.swing.JComboBox jComboBoxFrom;
    private javax.swing.JComboBox jComboBoxPriorities;
    private javax.swing.JComboBox jComboBoxTo;
    private javax.swing.JFormattedTextField jFormattedTextFieldVolume;
    private javax.swing.JFormattedTextField jFormattedTextFieldWeight;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelDuration;
    private javax.swing.JLabel jLabelPriceInfo;
    private javax.swing.JTextField jTextFieldDay;
    // End of variables declaration//GEN-END:variables
}
